####################
#MAP French Algeria#
####################
#This to obtain:
#Figure_A21.jpg

rm(list = ls())
library("sf")
library("ggplot2")
library("dplyr")
library("ggrepel")
library("RColorBrewer")
library("lemon")
library("maptools")
library("ggsn")
library("ggplot2")
library("dplyr")
library("lemon")
library("raster")
library("readxl")

setwd("/Users/bgpopescu/Dropbox/")
#setwd("C:/Users/bogdanp/Dropbox/")

#Reading polygons
colonies = read_excel("./Legacies_Project/Analysis/data/algerian_colonies_native_vs_indegen.xlsx", sheet=1, col_names = TRUE, skip = 0)
names(colonies)

all_coun <- st_read(dsn="./Legacies_Project/Analysis/data/data.gdb",
                          layer="all_countries_GCS")
#Reading raster
euro_raster <- raster("./Legacies_Project/Analysis/data/euro_raster.tif")

#Preparing raster
max_lat<-max(colonies$lat) + 2
min_lat<-min(colonies$lat) - 2

max_lon<-max(colonies$lon) + 2
min_lon<-min(colonies$lon) - 2

e <- extent(min_lon-4, max_lon+4, min_lat-4, max_lat+4)
rc <- crop(euro_raster, e)	

#Lower resolution raster
rc.aggregate <- aggregate(rc, fact=5)
res(rc.aggregate)

#Get rid of really high mountains
rc <- reclassify(rc.aggregate, c(10000,Inf,NA))
rm(rc.aggregate)
rna <- reclassify(rc, cbind(NA, 0))
rm(rc)

#Convert rasters TO dataframes for plotting with ggplot
hdf <- rasterToPoints(rna); hdf <- data.frame(hdf)
colnames(hdf) <- c("X","Y","Elev")


map<-ggplot()+
  geom_raster(data=hdf,aes(X,Y,alpha=Elev)) +
  scale_alpha(name = "Altitude", guide = "none")  + 
  geom_sf(data = all_coun, fill=NA, color = "black", size = 0.4)+
  geom_point(data=colonies, 
             aes(x =lon, y= lat, 
                 color=factor(admin)), size=3, alpha=0.5)+
  scale_color_manual(name = "Civilian/Military",
                     values=c("blue", "red"),
                     label=c("Civilian", "Military"))+
  coord_sf(ylim=c(min_lat,max_lat), xlim=c(min_lon, max_lon))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        #Legend.position values should be between 0 and 1. c(0,0) corresponds to the "bottom left"
        #and c(1,1) corresponds to the "top right" position.
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12),
        aspect.ratio = 1)+
        ggsn::scalebar(x.min = min_lon, x.max = max_lon,
           #Above you mention how long the scalebar should be
           y.min = min_lat+6, y.max = max_lat-2,
           #Above you mention how thick the scalebar should be
           dist = 100, dist_unit = "km",
           transform = T, model = "WGS84",
           location = "topright",
           st.size = 4,
           #Above you have the font size of the numbers below the scalebar
           st.dist =0.05,
           #Above you have the distance between the bar and the text, as a proportion of the y axis.
           height=0.05)+
  geom_label_repel(data=colonies, aes(label=name, x = lon, y = lat,
                                 fontface = 'bold'), alpha = 0.6)

map<-reposition_legend(map, 'bottom left')
ggsave(map, file="./Legacies_Project/Paper/figures/Figure_A21.jpg", 
       height=19.42, width=20.2, units = "cm", dpi=300)

##########
#FOR BOOK#
##########


ggsave(map, 
       file = './Book Project/Book_manuscript_RR/chapter_07_graphs/figure_7_3_300dpi.jpg', 
       height=19.42, width=20.2, units = "cm", dpi=300)
ggsave(map, 
       file = './Book Project/Book_manuscript_RR/chapter_07_graphs/figure_7_3_1200dpi.jpg', 
       height=19.42, width=30.2, units = "cm", dpi=1200)


